package com.lite.api.controller.publics.contract;


import com.lite.business.convert.contract.ContractConvert;
import com.lite.business.entity.contract.Contract;
import com.lite.business.entity.contract.ContractQueryWrapper;
import com.lite.business.exception.BusinessException;
import com.lite.business.service.contract.ContractService;
import com.lite.business.vo.contract.ContractVO;
import com.lite.common.dto.ResultResponse;
import com.lite.common.utils.ResultResponseUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author stranger
 * @since 2022-10-24
 */
@RestController
@RequestMapping("/contract")
public class ContractController {
    @Autowired
    ContractService contractService;

    @Autowired
    ContractConvert contractConvert;

    /**
     * 获取一个订购合同信息
     *
     * @param contractId 合同ID
     * @return 合同对象
     * @throws BusinessException 业务异常
     */
    @GetMapping("/getContractInfoById")
    public ResultResponse<ContractVO> getContractInfoById(@RequestParam @NotNull Integer contractId) throws BusinessException {
        return ResultResponseUtils.success(contractService.getContractInfoById(contractId), "获取合同信息成功");
    }

    /**
     * 分页获取订购合同信息列表
     *
     * @param page                 页数
     * @param size                 容量
     * @param contractQueryWrapper 条件查询包装类
     * @return 合同信息列表
     * @throws BusinessException 业务异常
     */
    @PostMapping("/listContractByPage")
    public ResultResponse<List<Contract>> listContractByPage(
            @RequestParam @NotNull Integer page,
            @RequestParam @NotNull Integer size,
            @RequestParam @NotNull ContractQueryWrapper contractQueryWrapper) throws BusinessException {
        return ResultResponseUtils.success(contractService.listContractByPage(page, size, contractQueryWrapper), "获取合同信息列表成功");
    }

    /**
     * 更新一个合同信息
     *
     * @param contractId 合同ID
     * @param contract   合同对象
     * @return 是否更新成功
     * @throws BusinessException 业务异常
     */
    @PostMapping("/updateContractInfoById")
    public ResultResponse<Boolean> updateContractInfoById(
            @RequestParam @NotNull Integer contractId,
            @RequestParam @NotNull Contract contract) throws BusinessException {
        return ResultResponseUtils.success(contractService.updateContractInfoById(contractId, contract), "更新合同信息成功");
    }

    /**
     * 删除一个合同信息
     *
     * @param contractId 合同ID
     * @return 是否删除成功
     * @throws BusinessException
     */
    @DeleteMapping("/removeContractInfoById")
    public ResultResponse<Boolean> removeContractInfoById(@RequestParam @NotNull Integer contractId) throws BusinessException {
        return ResultResponseUtils.success(contractService.removeContractInfoById(contractId), "删除合同信息成功");
    }

    /**
     * 新增一个合同信息
     *
     * @param contractVO 合同视图层对象
     * @return 是否新增成功
     * @throws BusinessException 业务异常
     */
    @PostMapping("/addContractInfo")
    public ResultResponse<Boolean> addContractInfo(@RequestBody @NotNull ContractVO contractVO) throws BusinessException {
        return ResultResponseUtils.success(contractService.addContractInfo(contractVO), "新增合同信息成功");
    }

}
